﻿using System;
using System.Runtime.InteropServices;
using Extensibility;
using Excel = Microsoft.Office.Interop.Excel;

namespace MyExcelAddin
{
    [ComVisible(true)]
    [Guid("2E2AA223-E340-4027-9283-6FD31E74CA99")] // 替换为你的GUID
    [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
    public interface IMyAddin
    {
        [DispId(1)]
        void SayHello();

        // 可以添加更多自定义方法
    }

    [ComVisible(true)]
    [Guid("E439D7D8-CC6F-439E-93C4-F6CC78235483")] // 替换为不同的GUID
    [ClassInterface(ClassInterfaceType.None)]
    public class MyAddin : IDTExtensibility2, IMyAddin
    {
        private Excel.Application excelApp;

        // 当加载项被加载时调用
        public void OnConnection(object Application, ext_ConnectMode ConnectMode, object AddInInst, ref Array custom)
        {
            excelApp = Application as Excel.Application;
            // 可以在这里初始化你的功能
        }

        // 当加载项被卸载时调用
        public void OnDisconnection(ext_DisconnectMode RemoveMode, ref Array custom)
        {
            // 清理资源
            excelApp = null;
        }

        // 当添加/删除文档时调用
        public void OnAddInsUpdate(ref Array custom)
        {
            // 可以处理添加项更新事件
        }

        // 当Excel启动时调用
        public void OnStartupComplete(ref Array custom)
        {
            // 可以在这里执行启动完成后的操作
        }

        // 当Excel关闭时调用
        public void OnBeginShutdown(ref Array custom)
        {
            // 可以在这里处理关闭前的操作
        }

        // 自定义方法实现
        public void SayHello()
        {
            if (excelApp != null)
            {
                Excel.Workbook wb = excelApp.ActiveWorkbook;
                if (wb != null)
                {
                    Excel.Worksheet ws = wb.ActiveSheet as Excel.Worksheet;
                    if (ws != null)
                    {
                        ws.Cells[1, 1].Value = "Hello from Excel Addin!";
                    }
                }
            }
        }
    }

    // 必须实现的GUID属性
    [ComVisible(true)]
    [Guid("BFF5173E-3D66-462B-B1B2-CD0C16CED52C")]// 替换为你的加载项GUID
    [ProgId("MyExcelAddin.Connect")]
    public class Connect : Object, IDTExtensibility2, IMyAddin
    {
        private MyAddin myAddin = new MyAddin();

        public void OnConnection(object Application, ext_ConnectMode ConnectMode, object AddInInst, ref Array custom)
        {
            myAddin.OnConnection(Application, ConnectMode, AddInInst, ref custom);
        }

        public void OnDisconnection(ext_DisconnectMode RemoveMode, ref Array custom)
        {
            myAddin.OnDisconnection(RemoveMode, ref custom);
        }

        public void OnAddInsUpdate(ref Array custom)
        {
            myAddin.OnAddInsUpdate(ref custom);
        }

        public void OnStartupComplete(ref Array custom)
        {
            myAddin.OnStartupComplete(ref custom);
        }

        public void OnBeginShutdown(ref Array custom)
        {
            myAddin.OnBeginShutdown(ref custom);
        }

        public void SayHello()
        {
            myAddin.SayHello();
        }
    }

    [ComVisible(true)]
    [Guid("E1B8F3A2-4C5D-4F6E-8A7B-9C0D1E2F3A4B")] // 接口GUID
    [InterfaceType(ComInterfaceType.InterfaceIsDual)] // 支持IDispatch和IUnknown
    public interface IMyComInterface
    {
        int Add(int a, int b);
        string GetMessage();
        DateTime GetCurrentDate();
        int Counter { get; set; } // 属性
    }
    [ComVisible(true)]
    [Guid("C2D9A1B4-3E2F-4D1C-9B8A-7C6D5E4F3A2B")] // 类GUID
    [ClassInterface(ClassInterfaceType.None)] // 不自动生成接口
    [ProgId("COMDemo.MyComClass")] // 设置ProgID
    [ComDefaultInterface(typeof(IMyComInterface))] // 指定默认接口
    [ComSourceInterfaces(typeof(IMyComEvents))] // 事件接口
    public class MyComClass : IMyComInterface
    {
        private int _counter;

        public int Add(int a, int b)
        {
            return a + b;
        }

        public string GetMessage()
        {
            return "Hello from C# COM DLL!";
        }

        public DateTime GetCurrentDate()
        {
            return DateTime.Now;
        }

        public int Counter
        {
            get => _counter;
            set => _counter = value;
        }

        // 事件实现
        public event Action<int> ValueChanged;

        public void RaiseEvent(int value)
        {
            ValueChanged?.Invoke(value);
        }
    }

    [ComVisible(true)]
    [Guid("E1B8F3A2-4C5D-4F6E-8A7B-9C0D1E2F3A4C")] // 事件接口GUID
    [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)] // 事件接口使用IDispatch
    public interface IMyComEvents
    {
        [DispId(1)]
        void OnValueChanged(int newValue);
    }
}